<?php

class AppBaseController extends Zend_Controller_Action
{
	public $limit = 5;
	public $request;
	public $form;
	public $controller;
	public $action;
	public $usuario;
	public $base = "cafe";
	public $translate;
	
	public function init()
	{
		Zend_Layout::startMvc();
		/**
		 * Tradutor
		 */
		
		
		/*$ln = $this->_request->getParam("ln");
		
		if(!$ln)
		{
			$ln = "en";
		}
		
		$translate = new Zend_Translate('tmx', 'languages.tmx', $ln);
		$this->translate = $translate;
		$this->view->translate = $translate;*/
		
		//$this->base .= "/index/index/ln/$ln";

		$this->__getUrlAction();		
		//$this->getIdentity();
		
		//$this->view->projeto    = $this->projeto;
		$this->view->base       = "/$this->base";
		/*$this->view->controller = $this->controller;
		$this->view->action     = $this->action;
		$this->view->usuario    = $this->usuario;
		
		$this->permissaoalt();
		//$this->historico();
		$this->agenda();
		$this->dataP();
		$this->log();
		
		//echo $this->controller . $this->action;
		
		if($this->controller != "index")
		{
			$this->_helper->layout->disableLayout();
		}*/
		//$this->menu();
	}
	
	public function permissaoformAction()
	{
		$permissao = new Permissao();
		
		echo implode("|", $permissao->form);
		
		exit;
	}
	
	public function permissaoactionsAction()
	{
		$permissao = new Permissao();
		
		echo implode("|", $permissao->acoes);
		
		exit;
	}
	
	public function permissaoalt()
	{
		$p = new Permissao();
		
    	$msg = "<script>";
    	$msg .= "alert(\"Sem permiss�o.\");";
    	$msg .= "history.back()";
    	$msg .= "</script>";
    	
		foreach ($p->subMenu as $key => $val)
		{
			$act = split("-", $val);
			
			foreach ($act as $aa)
			{
				if($key == $this->controller && $aa == $this->action)
				{
					die($msg);
				}
			}
		}
	}
	
	public function preDispatch()
	{
		$this->initView();		
	}

	public function __getUrlAction()
	{
		$parans = $this->_request->getParams();
		$url    = '';
		
		foreach ($parans as $key => $value)
		{
			if($key == 'module')
			{
				$value = ($value == 'default')?'':$value.'/';
				$url   = $value .$url;
				
				continue;
			}
			else if($key == 'controller')
			{
				$url .= $value.'/';
								
				$this->controller = $value;
			
				continue;
			}
			else if($key == 'action')
			{
				$url .= $value.'/';
				
				$this->action = $value;
				
				continue;
			}
			else if($key == 'pagina')
				continue;
			$url .= $key.'/'.$value.'/';
		}

		return $url;
	}

	public function menu()
	{
		if($this->controller != "login")
		{

		}
	}
	
	public function grid($class)
	{
		$grid = new Grid($class);
		
		$grid->select();
				
		$this->view->fields    = $grid->run();
		$this->view->head      = $grid->fields;
		$this->view->primary   = $grid->primary;
		$this->view->status    = $grid->status;
		$this->view->paginacao = $grid->paginacao;
		
		/**
		 * Tabela de busca.
		 */
		
		$this->view->table = $class;
	}
	
	public function getIdentity()
	{
		$session = "usuario_" . AppBaseController::$session;
		
		$usuario = new Zend_Session_Namespace($session);
		
		if(!$usuario->nome && $this->controller != "login")
		{
			$this->_redirect("/login/");
		}
		else
		{
			$this->usuario = $usuario;
			
			return $usuario->id;
		}
	}
	
	public function getArrCod($cod)
	{
		foreach (split(",", $cod) as $val)
		{
			$arr[$val] = $val;	
		}
		
		return $arr;
	}
	
	public function setParametros()
	{
		$Parametros = new Parametros();
		$parametro  = $Parametros->find(1)->current();

		Zend_Registry::set('qtdprodpagina', 20);
		$this->emailprincipal = $parametro->emailprincipal;
		$this->view->urlloja  = $parametro->url;
		Zend_Registry::set('url', $parametro->url);
		$this->view->nomeloja       	= $parametro->nomeloja;
		$this->view->razaosocial    	= $parametro->razaosocial;
		$this->view->enderecocompleto   = $parametro->enderecocompleto;
		$this->view->telefonesac    	= $parametro->telefonesac;
		$this->view->cep		    	= $parametro->cep;
	}
	
	public function log()
	{
		$txt = "$this->controller - $this->action.";
		
		if($txt == "index - index." || $txt == "logoff - index.")
		{
			$txt = "";
		}

		Log::write($txt, $this->usuario->nome, $this->_request->isPost());
	}
	
	public function ajaxatualizafieldAction()
	{
		$table   = $this->_request->getParam("table");
		$field   = $this->_request->getParam("field");
		$id      = $this->_request->getParam("id");
		$val     = $this->_request->getParam("val");
		$primary = $table . "_cod";
		
		$obj = new $table;
		
		$db = $obj->getAdapter();
		
		$db->query("UPDATE $table SET $field = '$val' WHERE $primary = $id");
		
		echo "UPDATE $table SET $field = '$val' WHERE $primary = $id";
		
		exit;
	}
	
	public function dataP()
	{
		$date = new Zend_Date();
		
		$this->view->data = $date->get(Zend_Date::DAY) . "/" . $date->get(Zend_Date::MONTH) . "/" . $date->get(Zend_Date::YEAR) . " " . $date->get(Zend_Date::HOUR) . ":" . $date->get(Zend_Date::MINUTE);
	}
	
	public function agenda()
	{
    	$INT006 = new INT006;
    	
    	/*if($this->_request->isPost())
    	{
	    	$msg = $this->_request->getPost("msg"); 
	    	$pon = split(",", $this->_request->getPost("ponto"));
	    	
	    	$INT010->setINT010_msg($msg);
			$INT010->setINT010_nome($this->usuario->nome);
			$INT010->setINT010_lat($pon[0]);
			$INT010->setINT010_lng($pon[1]);
			
			$INT010->save();
    	}*/
		
		$this->view->ativ_ = $INT006->getWidget();
	}
	
	public function historico()
	{
		$i = 0; 	
		$at = "$this->controller/$this->action";	
		$flag = false;
		
		if(!is_array($_SESSION["historico"]))
		{
			$_SESSION["historico"] = array();
		}
		
		foreach ($_SESSION["historico"] as $val)
		{
			if($val == "index/index" || $val == "login/index")
			{
				continue;
			}
			
			$arr[] = $val;
			
			if($val == $at)
			{
				$flag = true;
			}
			
			$i++;
		}
		
		if(!$flag)
		{
			$arr[] = $at;
			
			$_SESSION["historico"] = $arr;
		}

		

		//setcookie("historico", serialize($arr), time() + 360000);
	}
}

?>